package cn.whuc.test

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object Demo01 {
  def main(args: Array[String]): Unit = {
    val conf: SparkConf = new SparkConf().setAppName("wordcount").setMaster("local[*]")
    val sc: SparkContext = new SparkContext(conf)
    val data: RDD[String] = sc.textFile("input/word.txt")
    val word: RDD[String] = data.flatMap(line  => line.split(" "))
    val wordToone: RDD[(String, Int)] = word.map(word => (word, 1))
    val wordCount: RDD[(String, Int)] = wordToone.reduceByKey((x:Int, y: Int) => {
      x + y
    })
    wordCount.collect().foreach(println)

    sc.stop()
  }

}
